<?php


namespace App\Http\Controllers\Customer;


use App\Http\Controllers\Controller;
use App\Http\Requests\Customer\Address\StoreRequest;
use App\Http\Requests\Customer\Address\UpdateRequest;
use App\Http\Searchs\Customer\AddressSearch;
use App\Repositories\CustomerAddress;

class AddressController extends Controller
{
    /**
     * @var CustomerAddress
     * @author luffyzhao@vip.126.com
     */
    private $address;

    /**
     * AddressController constructor.
     * @param CustomerAddress $address
     * @author luffyzhao@vip.126.com
     */
    public function __construct(CustomerAddress $address)
    {
        $this->address = $address;
    }

    /**
     * @param AddressSearch $search
     * @return \Illuminate\Http\JsonResponse
     * @author luffyzhao@vip.126.com
     */
    public function index(AddressSearch $search)
    {
        return $this->response(
            $this->address->paginate($search->toArray())
        );
    }

    /**
     * @param StoreRequest $request
     * @return \Illuminate\Http\JsonResponse
     * @author luffyzhao@vip.126.com
     */
    public function store(StoreRequest $request)
    {
        return $this->response(
            $this->address->create($request->only([
                'customer_id', 'express_id', 'areas', 'address', 'phone', 'contacts', 'remark'
            ]))
        );
    }

    /**
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     * @author luffyzhao@vip.126.com
     */
    public function edit($id)
    {
        return $this->response(
            $this->address->find($id)
        );
    }

    /**
     * @param UpdateRequest $request
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     * @throws \Throwable
     * @author luffyzhao@vip.126.com
     */
    public function update(UpdateRequest $request, $id)
    {
        return $this->response(
            $this->address->update($id, $request->only([
                'customer_id', 'express_id', 'areas', 'address', 'phone', 'contacts', 'remark'
            ]))
        );
    }
}